package com.emath.activities;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Point;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.Toast;
import com.emath.EnterUsername;
import com.lnl.R;
import com.emath.objects.BackgroundSoundService;
import com.emath.shared.Globals;

public class MainActivity extends Activity {

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splash);
		
//		WindowManager w = this.getWindowManager();
//		Display d = w.getDefaultDisplay();
//		DisplayMetrics metrics = new DisplayMetrics();
//		d.getMetrics(metrics);
//		// since SDK_INT = 1;
//		Globals.displayWidth = metrics.widthPixels;
//		Globals.displayHeight = metrics.heightPixels;
//		// includes window decorations (statusbar bar/menu bar)
//		if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17)
//			try {
//				Globals.displayWidth = (Integer) Display.class.getMethod("getRawWidth").invoke(d);
//				Globals.displayHeight = (Integer) Display.class.getMethod("getRawHeight").invoke(d);
//			} catch (Exception ignored) {
//			}
//		// includes window decorations (statusbar bar/menu bar)
//		if (Build.VERSION.SDK_INT >= 17)
//			try {
//			    Point realSize = new Point();
//			    Display.class.getMethod("getRealSize", Point.class).invoke(d, realSize);
//			    Globals.displayWidth = realSize.x;
//			    Globals.displayHeight = realSize.y;
//			} catch (Exception ignored) {
//			}
		
		try {
			Globals.backgroundMusicService = new Intent(this,
					BackgroundSoundService.class);
		} catch (Exception e) {
			e.printStackTrace();
			Toast.makeText(getApplicationContext(),
					e.getClass().getName() + " " + e.getMessage(),
					Toast.LENGTH_LONG).show();
		}

		if (!Globals.isMusicPlaying && Globals.settingsHolder[1] == 1) {
			startService(Globals.backgroundMusicService);
			Globals.isMusicPlaying = true;
		}
		// mServ.startActivity(getParentActivityIntent());
		Thread logoTimer = new Thread() {
			public void run() {
				try {
					ImageView logo = (ImageView) findViewById(R.id.logo);

					// Scaling
					Animation scale = new ScaleAnimation(0.1f,
							Animation.RELATIVE_TO_SELF, 0.1f,
							Animation.RELATIVE_TO_SELF);
					scale.setDuration(3000); // 3 Seconds
					AnimationSet animSet = new AnimationSet(true);
					animSet.setFillEnabled(true);
					animSet.addAnimation(scale);
					logo.startAnimation(animSet);

					sleep(5000);
					Intent menuIntent = new Intent(MainActivity.this,
							EnterUsername.class);
					Globals.focusedActivity = "enter_username";
					startActivity(menuIntent);
				} catch (InterruptedException e) {
					e.printStackTrace();
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					finish();
				}
			}
		};
		logoTimer.start();
	}

	@Override
	protected void onPause() {

		super.onPause();

	}
	@Override
	protected void onResume() {
		if (!Globals.isMusicPlaying) {
			if (Globals.settingsHolder[1] == 1) {
				startService(Globals.backgroundMusicService);
			}
			Globals.isMusicPlaying = true;
		}
		super.onResume();
	}

//	@Override
//	protected void onDestroy(){
//		stopService(Globals.backgroundMusicService);
//		Globals.isMusicPlaying = false;
//		super.onDestroy();
//	}
	
	public class BackgroundSound extends AsyncTask<Void, Void, Void> {

		@Override
		protected Void doInBackground(Void... params) {

			return null;
		}

	}

}
